import java.util.LinkedList;
import java.util.Random;

import javax.swing.JOptionPane;

public class Snake {
	public static final int UP = 0X100;
	public static final int RIGHT = 0x101;
	public static final int DOWN = 0x102;
	public static final int LETF = 0x103;
	private LinkedList<Node> body;
	private int direction; // 定义方向
	private Node food; // 定义食物
	private int counter = 0; // 定义分数
	private int spend; // 定义速度
	private String txtstate; // 定义运行状态
	private Boolean iseat = false; // 定义true时吃了食物
	private Boolean isquit = false; // 定义ture时退出线程
	private Boolean iscount = false; // 定义为ture时改变分数
	private Boolean isbuttonactive=false;

	public Snake() {
		isquit = false;
		body = new LinkedList<Node>();
		direction = RIGHT;
		initSnake();
	}

	// 初始化游戏
	public void initSnake() {
		body.clear();
		body.add(new Node(80, 20));
		body.add(new Node(60, 20));
		body.add(new Node(40, 20));
		direction = RIGHT;
		isbuttonactive=false;
		setIseat(false);
		setIsquit(true);
		setIscount(false);
		setTxtstate("运行中...");
		spend = 700;
		counter = 0;
		makeFood();
	}

	public LinkedList<Node> getBody() {
		return body;
	}

	// 制造一个食物
	public void makeFood() {
		Random rd = new Random();
		int x = rd.nextInt(25) * Node.WIDTH, y = rd.nextInt(25) * Node.HEIGHT;
		food = new Node(x, y);
	}

	// 判断运行方向
	public void change(int direction) {
		if (this.direction % 2 != direction % 2) {
			this.direction = direction;
		}
	}

	// 移动蛇体
	public void nodeMove() {
		iseat = false;
		Node tail = body.removeLast();
		Node head = body.getFirst();
		int x = head.getX(), y = head.getY();

		// 当超过边框时退出游戏
		if (x < 0 || y < 0||x>Node.WIDTH*25-15||y>Node.HEIGHT*25-15) {
			setIsquit(true); // 设置isquit为true时退出游戏
			setTxtstate("结束");// 将状态甚至为结束
			setIsbuttonactive(true);
			JOptionPane.showMessageDialog(null, "你的得分是：" + counter * 10 + "。\n" + "你撞墙了，小同志");// 显示得分和失败对话框
		}

		// 当舌头碰撞蛇体时退出游戏
		for (int i = 5; i < body.size(); i++) {
			Node node = body.get(i);
			int nodex = node.getX();
			int nodey = node.getY();
			if (x == nodex && y == nodey) {
				setIsquit(true);// 设置isquit为true时退出游戏
				setIsbuttonactive(true);
				setTxtstate("结束"); // 将状态甚至为结束
				JOptionPane.showMessageDialog(null, "你的得分是：" + counter * 10 + "分" + "\n" + "你撞到身体了，小同志，你失败了！");
				return;
			}
		}

		switch (direction) {
		case RIGHT:
			x += Node.WIDTH;
			break;
		case UP:
			y -= Node.HEIGHT;
			break;
		case LETF:
			x -= Node.WIDTH;
			break;
		case DOWN:
			y += Node.HEIGHT;
			break;
		default:
			break;
		}
		tail.setX(x);
		tail.setY(y);
		body.addFirst(tail);
	}

	// 吃食物
	public void eatFood() {
		int headx = body.getFirst().getX(), heady = body.getFirst().getY();
		int foodx = food.getX(), foody = food.getY();

		if (headx == foodx && heady == foody) {
			counter++;
			body.addFirst(food);
			setIscount(true); // 当为true时改变状态栏分数
			setIseat(true); // 当为true时可以调用makeFood()方法
		}
		System.out.println("counter" + counter);
	}

	
	public Boolean getIsbuttonactive() {
		return isbuttonactive;
	}

	public void setIsbuttonactive(Boolean isbuttonactive) {
		this.isbuttonactive = isbuttonactive;
	}

	public Boolean getIscount() {
		return iscount;
	}

	public void setIscount(Boolean iscount) {
		this.iscount = iscount;
	}

	public int getDirectoin() {
		return direction;
	}

	public void setDirectoin(int direction) {
		this.direction = direction;
	}

	public Node getFood() {
		return food;
	}

	public void setFood(Node food) {
		this.food = food;
	}

	public int getCounter() {
		return counter;
	}

	public void setCounter(int counter) {
		this.counter = counter;
	}

	public int getSpend() {
		return spend;
	}

	public void setSpend(int spend) {
		this.spend = spend;
	}

	public String getTxtstate() {
		return txtstate;
	}

	public void setTxtstate(String txtstate) {
		this.txtstate = txtstate;
	}

	public Boolean getIseat() {
		return iseat;
	}

	public void setIseat(Boolean iseat) {
		this.iseat = iseat;
	}

	public Boolean getIsquit() {
		return isquit;
	}

	public void setIsquit(Boolean isquit) {
		this.isquit = isquit;
	}

	public void setBody(LinkedList<Node> body) {
		this.body = body;
	}	
}
